﻿/*
Copyright (c) 2008 William Duff

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsLive.PublishPlugins;
using System.Diagnostics;
using LiveUpload.Facebook.Properties;

namespace LiveUpload.Facebook
{
    public partial class FacebookVideoPanel : Panel
    {
        #region Constructors

        /// <summary>
        /// Constructs a new FacebookAlbumRadioButton to represent an album that can be selected
        /// as part of the upload process.
        /// </summary>
        public FacebookVideoPanel()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        #endregion

        #region Private Fields

        private Item video;
        private const int PADDING = 10;

        #endregion

        #region Public Fields

        /// <summary>
        /// The video that this radio button represents.
        /// </summary>
        public Item Video
        {
            get { return video; }
            set
            {
                if (video != value)
                {
                    video = value;
                    OnVideoChanged(new EventArgs());
                }
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// This event is fired when the video associated with this radio button is changed.
        /// </summary>
        private void OnVideoChanged(EventArgs eventArgs)
        {
            if (video != null)
            {
                // Description must be set first because of event handlers for the TextBoxes.
                if (!string.IsNullOrEmpty(video.Title))
                {
                    this.facebookVideoDescriptionTextBox.Text = video.Title;
                }
                this.facebookVideoDescriptionTextBox.Show();

                // Now set the title.
                this.facebookVideoTitleTextBox.Text = 
                    video.OriginalFileName.Remove(video.OriginalFileName.LastIndexOf('.'));
                this.facebookVideoTitleTextBox.Show();

                // Now set the description.
                if (FacebookPublishPlugin.InvokedByMovieMaker())
                {
                    this.facebookVideoDescriptionTextBox.Text = Resources.MadeWithMovieMaker;
                }

                // Hide the size label if video size cannot be completely determined.
                // MovieMaker Beta seems to set the default video size to 4096 bytes.
                if (video.FileSize <= 4096)
                {
                    this.facebookVideoSizeLabel.Height = 0;
                }
                else
                {
                    // Set the size.
                    double videoSize = Math.Round(video.FileSize / (1024 * 1024), 2);
                    string sizeUnit = " MB";

                    if (videoSize >= 1024)
                    {
                        videoSize = Math.Round(videoSize / 1024, 2);
                        sizeUnit = " GB";
                    }

                    this.facebookVideoSizeLabel.Text = string.Format(this.facebookVideoSizeLabel.Text,
                        videoSize + sizeUnit);
                }

                // Update the UI.
                LayoutVideoInformation();
            }
        }

        /// <summary>
        /// Resizes and realigns any labels as necessary.
        /// </summary>
        private void LayoutVideoInformation()
        {
            this.SuspendLayout();

            this.Height = this.facebookVideoSizeLabel.Bottom + PADDING;
            this.facebookVideoBorderBottom.Top = this.Height - this.facebookVideoBorderBottom.Height;

            this.ResumeLayout(true);
        }

        /// <summary>
        /// Keeps the video title up to date.
        /// </summary>
        private void facebookVideoTitleTextBox_TextChanged(object sender, EventArgs e)
        {
            video.Title = this.facebookVideoTitleTextBox.Text;
        }

        /// <summary>
        /// Keeps the video description up to date.
        /// </summary>
        private void facebookVideoDescriptionTextBox_TextChanged(object sender, EventArgs e)
        {
            video.Description = this.facebookVideoDescriptionTextBox.Text;
        }

        #endregion
    }
}
